/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ibrae.excelparser1.mainlogics.config;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import org.apache.commons.io.FileUtils;

import ru.ibrae.excelparser1.excelstorageentities.entities.MyExcelCell;
import ru.ibrae.excelparser1.mainlogics.Logging.ILogger;
import ru.ibrae.excelparser1.mainlogics.Logging.implimentation.ComplexLogger;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author pasha
 */
public class ConfigurationWorker {
    //~ Static fields/initializers ---------------------------------------------

    private static final XStream STREAM = new XStream(new DomDriver());
    private static final ILogger logger = new ComplexLogger();
    private static final File config    = new File(".excelparser/config.xml");

    //~ Methods ----------------------------------------------------------------

    public static void writeConfiguration(Configuration conf) {
        STREAM.alias("conf", Configuration.class);

        File dir = new File(".excelparser");

        if(!dir.exists()) {
            dir.mkdir();
        }

        if(!config.exists()) {
            try {
                config.createNewFile();
            } catch(IOException ex) {
                Logger.getLogger(ConfigurationWorker.class.getName()).log(
                    Level.SEVERE,
                    null,
                    ex);

                logger.log(
                    "Предупреждение. Невозможно создать файл конфигурации. Конфигурация не будет сохранена");
            }
        }

        try {
            FileUtils.writeStringToFile(config, STREAM.toXML(conf));
        } catch(IOException ex) {
            Logger.getLogger(ConfigurationWorker.class.getName()).log(Level.SEVERE,
                                                                      null,
                                                                      ex);
            logger.log("Предупреждение. Невозможно сохранить путь.");
        }
    }

    public static Configuration readConfiguration() {
        STREAM.alias("conf", Configuration.class);

        if(config.exists()) {
            try {
                return ( Configuration )STREAM.fromXML(
                    FileUtils.readFileToString(config));
            } catch(IOException ex) {
                Logger.getLogger(ConfigurationWorker.class.getName()).log(
                    Level.SEVERE,
                    null,
                    ex);
            }
        }

        return null;
    }

    public static ArrayList<MyExcelCell> getCorrectList2Header()
        throws IOException
    {
        InputStream is = FileUtils.class.getResourceAsStream(
            "/CorrectStructure.xml");
        STREAM.alias("cell", MyExcelCell.class);

        return ( ArrayList<MyExcelCell> )STREAM.fromXML(is);
    }

    public static ArrayList<MyExcelCell> getCorrectList1Sctructure() {
        STREAM.alias("cell", MyExcelCell.class);

        return ( ArrayList<MyExcelCell> )STREAM.fromXML(
            FileUtils.class.getResourceAsStream("/CorrectList1Structure.xml"));
    }
}
